package com.songcha.library_common.ui.view

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.songcha.library_common.R
import com.songcha.library_common.util.ScreenUtil
import com.songcha.library_common.util.dp2px


class BottomNavigationView
@JvmOverloads
constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0,
    defRes: Int = 0
) : LinearLayout(context, attrs, defStyle, defRes) {

    interface IOnBottomNavigationItemChangeListener{
        fun onBottomNavigationItemChange(position:Int,tag:String)
    }

    private var NAVIGATION_DEFAULT_HEIGHT=60.dp2px()
    private var mVp:ViewPager?=null
    private var mItemChangeListener:IOnBottomNavigationItemChangeListener?=null
    private var mCurrentPosition=0
    private var mBackgroundColor=Color.WHITE
    private var mCurrentTag=""

    init {
        initView(context,attrs)
    }

    private fun initView( context: Context, attrs: AttributeSet?, ){
        if(attrs!=null){
            val ta = context.obtainStyledAttributes(attrs, R.styleable.BottomNavigationView)
            mBackgroundColor=ta.getColor(R.styleable.BottomNavigationView_bottom_navigation_backgroundColor,Color.WHITE)
            ta.recycle()
        }

        setBackgroundColor(mBackgroundColor)

        orientation=HORIZONTAL

        //selectItem(mCurrentPosition)

    }

    //may lost data when start with hot
    fun selectCurrent(){
        select(mVp?.currentItem?:0)
    }

    fun select(position: Int){
        if(mCurrentPosition==position) return
        mCurrentPosition=position
        mVp?.currentItem=mCurrentPosition
        for(i in 0 until childCount){
            val itemView=getChildAt(i)
            if(itemView is BottomNavigationItemView){
                if(i==mCurrentPosition){
                    itemView.onSelect()
                }else{
                    itemView.onUnSelect()
                }
            }

        }
    }

    private fun setItemOnclickListener(itemView:BottomNavigationItemView,position: Int){
        itemView.setOnClickListener{
            //LogUtil.log("aaa",mCurrentPosition,position,mVp?.currentItem)

            if(mCurrentPosition==position) return@setOnClickListener

            mVp?.currentItem=position

            /* mCurrentPosition=position
             mCurrentTag=(getChildAt(position) as BottomNavigationItemView).TAG
             mItemChangeListener?.onBottomNavigationItemChange(mCurrentPosition,mCurrentTag)*/

            itemView.onSelect()

            for(i in 0 until childCount){
                if(i==position) continue
                val itemView2=getChildAt(i)
                itemView2 as BottomNavigationItemView
                itemView2.onUnSelect()
            }

        }

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        val heightMode=MeasureSpec.getMode(heightMeasureSpec)
        val heighSize=MeasureSpec.getSize(heightMeasureSpec)
        if(heightMode==MeasureSpec.EXACTLY){
            setMeasuredDimension(ScreenUtil.getScreenWidth(context),heighSize)
        }else{
            setMeasuredDimension(ScreenUtil.getScreenWidth(context),NAVIGATION_DEFAULT_HEIGHT)
        }

    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)
        if(childCount<1){
            throw Throwable("BottomNavigationView must has 1 child at least")
        }
        if(childCount>51){
            throw Throwable("BottomNavigationView can only has 5 child at most")
        }
        val itemWidth=ScreenUtil.getScreenWidth(context)/childCount
        for(i in 0 until childCount){
            val itemView=getChildAt(i)
            itemView.layout(i*itemWidth,0,(i+1)*itemWidth,measuredHeight)
        }

    }

    fun setOnBottomNavigationItemChangeListener(listener: IOnBottomNavigationItemChangeListener){
        mItemChangeListener=listener
    }

    fun setViewPager(vp:ViewPager){
        mCurrentPosition=vp.currentItem
        vp.offscreenPageLimit=childCount
        mVp=vp

        for(i in 0 until childCount){
            val itemView=getChildAt(i)
            if(itemView !is BottomNavigationItemView){
                throw Throwable("BottomNavigationView child's type must be BottomNavigationView")
                break
            }else{
                //select first default
                if(mCurrentPosition==i)
                    itemView.onSelect()
                else
                    itemView.onUnSelect()

                setItemOnclickListener(itemView,i)
            }
        }

        mVp!!.clearOnPageChangeListeners()
        mVp!!.addOnPageChangeListener(object : OnPageChangeListener{
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                mCurrentPosition=position
                mCurrentTag=(getChildAt(position) as BottomNavigationItemView).TAG
                mItemChangeListener?.onBottomNavigationItemChange(mCurrentPosition,mCurrentTag)
            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })

    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        selectCurrent()
    }

}